<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">

<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <title>物资信息</title>
    <meta content="width=device-width, initial-scale=1" name="viewport">
    <link th:include="admin/adminGoodsUtils-common::commonheader">
    <style>
        .layui-table-cell {
            height: 150px;
            line-height: 150px;
        }
        .layui-table img {
            max-width: 250px;
        }
        .layui-table-tool {
            z-index: 10;
        }
    </style>
</head>

<body>
<!--wrapper-->
<div class="wrapper">
    <div th:replace="admin/adminGoodsUtils-common::#commonbody1"></div>
    <div th:replace="admin/adminGoodsUtils-common::#commonbody2"></div>
    <div class="page-wrapper">
        <div class="page-content  col-lg-pull-2">
            <script id="toolbarDemo" type="text/html">
                <div class="layui-btn-container layui-col-md-offset5">
                    <button class="layui-btn layui-btn-sm" lay-event="getCheckData">下架选择的全部商品</button>
                    <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
                    <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
                </div>
            </script>
            <table align="center" class="layui-hide col-lg-offset-2 fs-lg-3" id="test" lay-filter="test"></table>
            <script id="barDemo" type="text/html">
                <a class="layui-btn layui-btn-normal" lay-event="update" style="text-decoration:none">更新商品</a>
                <a class="layui-btn layui-btn-danger" lay-event="del" style="text-decoration:none">立即下架</a>
            </script>
            <!--把这个img标签插入我们的表格中-->
            <script id="img" src="text/html">
                <img src="/img/products/{{d.imgId}}" style="height: 100%;width: 100%"/>
            </script>
        </div>
    </div>
</div>
<!--end wrapper-->
<div th:include="admin/adminGoodsUtils-common::#commontail"></div>
<script>
    layui.use('table', function () {
        var table = layui.table;

        //温馨提示：默认由前端自动合计当前行数据。从 layui 2.5.6 开始： 若接口直接返回了合计行数据，则优先读取接口合计行数据。
        //详见：https://www.layui.com/doc/modules/table.html#totalRow
        table.render({
            elem: '#test',
            url: '/doGovernment-supply',
            toolbar: '#toolbarDemo',    /*绑定三个按钮*/
            title: '政府提供物品列表',         /*导出表格时表格的名称*/
            cols: [
                [{
                    type: 'checkbox',
                }, {
                    field: 'id',
                    title: '编号',
                    width: 150,
                    sort: true,
                    align: 'center',
                }, {
                    field: 'adminGoodsUtils',
                    title: '名称',
                    width: 250,
                    edit: 'text',
                    align: 'center',
                }, {
                    field: 'number',
                    title: '数量',
                    width: 200,
                    sort: true,
                    edit: 'text',
                    align: 'center',
                }, {
                    field: 'imgId',
                    title: '实物图片',
                    width: 400,
                    templet: '#img',  //插入图片
                    align: 'center',
                }, {
                    title: '操作',
                    toolbar: '#barDemo',
                    width: 300,
                }]
            ],
            page: true
        });

        //工具栏事件
        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'getCheckData':
                    layer.confirm('确认下架这些商品？', function () {
                        var data = checkStatus.data;
                        var _list="";
                        for(let i=0;i<data.length;i++){
                            _list=_list+data[i].id+",";
                            if (i==data.length-1){
                                _list=_list+data[i].id;
                            }
                        }
                        console.log(_list)
                        $.ajax({
                            type: 'post',
                            url: '/deleteAllSelectSupply',
                            data:{ "allSupplyId": _list },
                            success: function (msg) {
                                if (msg.code === 200) {
                                    layer.msg("[提示]"+"全部下架成功");
                                    window.location.href = "/adminGoodsUtils-supply";
                                }else {
                                    layer.msg("[提示]"+"部分下架失败");
                                }
                            }
                        })
                    });

                    break;
                case 'getCheckLength':
                    var data = checkStatus.data;
                    layer.msg('选中了：' + data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选' : '未全选')
                    break;
            }
            ;
        });

        table.on('tool(test)', function (obj) {
            var data = obj.data;
            console.log(data)
            if (obj.event === 'del') {
                layer.confirm('确认下架该商品？', function (index) {
                    console.log(data.id)
                    window.location = "/deleteSupply?id=" + data.id;
                });
            } else if (obj.event === 'update') {
                layer.confirm('确认更新该商品的信息？', function (index) {
                    layer.close(index);
                    window.location = "/updateSupply?id=" + data.id + "&number=" + data.number+"&adminGoodsUtils="+data.adminGoodsUtils;
                });
            }
        });
    });
</script>
</body>

</html>
